using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace phasor
{
	/// <summary>
	/// Summary description for PositionForm.
	/// </summary>
	public class PositionForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.NumericUpDown unmNSlits;
		private System.Windows.Forms.NumericUpDown numPhase;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;


		public Form1 form1;
		public bool Closed;

		public PositionForm() {
			InitializeComponent();
			

		}

		protected override bool ProcessDialogKey(Keys key) {
			return form1.ProcessDialogKey_Internal(key);
		}


		public void SetPositionOnOwner() {
			this.Location = form1.PointToScreen(new Point(87, 25));
		}

		#region Generated Code

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.unmNSlits = new System.Windows.Forms.NumericUpDown();
			this.numPhase = new System.Windows.Forms.NumericUpDown();
			((System.ComponentModel.ISupportInitialize)(this.unmNSlits)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.numPhase)).BeginInit();
			this.SuspendLayout();
			// 
			// unmNSlits
			// 
			this.unmNSlits.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.unmNSlits.Location = new System.Drawing.Point(0, 0);
			this.unmNSlits.Maximum = new System.Decimal(new int[] {
																	  10000,
																	  0,
																	  0,
																	  0});
			this.unmNSlits.Minimum = new System.Decimal(new int[] {
																	  2,
																	  0,
																	  0,
																	  0});
			this.unmNSlits.Name = "unmNSlits";
			this.unmNSlits.Size = new System.Drawing.Size(70, 20);
			this.unmNSlits.TabIndex = 29;
			this.unmNSlits.Value = new System.Decimal(new int[] {
																	2,
																	0,
																	0,
																	0});
			this.unmNSlits.ValueChanged += new System.EventHandler(this.unmNSlits_ValueChanged);
			this.unmNSlits.Leave += new System.EventHandler(this.unmNSlits_Leave);
			// 
			// numPhase
			// 
			this.numPhase.DecimalPlaces = 5;
			this.numPhase.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.numPhase.Increment = new System.Decimal(new int[] {
																	   1,
																	   0,
																	   0,
																	   327680});
			this.numPhase.Location = new System.Drawing.Point(0, 20);
			this.numPhase.Maximum = new System.Decimal(new int[] {
																	 99,
																	 0,
																	 0,
																	 0});
			this.numPhase.Minimum = new System.Decimal(new int[] {
																	 99,
																	 0,
																	 0,
																	 -2147483648});
			this.numPhase.Name = "numPhase";
			this.numPhase.Size = new System.Drawing.Size(70, 20);
			this.numPhase.TabIndex = 28;
			this.numPhase.ValueChanged += new System.EventHandler(this.numPhase_ValueChanged);
			this.numPhase.Leave += new System.EventHandler(this.numPhase_Leave);
			// 
			// PositionForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.Color.White;
			this.ClientSize = new System.Drawing.Size(69, 40);
			this.Controls.Add(this.unmNSlits);
			this.Controls.Add(this.numPhase);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "PositionForm";
			this.Opacity = 0.99;
			this.ShowInTaskbar = false;
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
			this.Closing += new System.ComponentModel.CancelEventHandler(this.PositionForm_Closing);
			this.Load += new System.EventHandler(this.PositionForm_Load);
			this.Deactivate += new System.EventHandler(this.PositionForm_Deactivate);
			((System.ComponentModel.ISupportInitialize)(this.unmNSlits)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.numPhase)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion
		
		
		#endregion
		


		private void PositionForm_Load(object sender, System.EventArgs e) {
			form1 = (Form1)this.Owner;

			SetPositionOnOwner();
			this.Width =69;
			
		}

		private void PositionForm_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
			Closed = true;
		}


		private bool nJustSet = false;
		private bool phaseJustSet = false;

		private void numSlitsCahnged() {
			form1._n = (int)unmNSlits.Value;
			return;
			
			if (form1 != null) {
				if (!nJustSet) {
					form1.n = (int)unmNSlits.Value;
				} else {
					nJustSet = false;
				}
			}
		}



		public void SyncNSlitsWithForm1() {
			//nJustSet = true;
			unmNSlits.Value = form1.n;
		}

		public void SyncPhaseWithForm1() {
			//phaseJustSet = true;
			numPhase.Value = (decimal)Form1.AlphaToPhi(form1.alpha, form1.n);
		}

		public void SetFocusOnNSlits() {
			unmNSlits.Focus();
		}
		public void SetFocusOnPhase() {
			numPhase.Focus();
		}



		private void unmNSlits_ValueChanged(object sender, System.EventArgs e) {
			numSlitsCahnged();
		}

		private void unmNSlits_Leave(object sender, System.EventArgs e) {
			numSlitsCahnged();
		}

		private void numPhaseCahnged() {
			if (form1 != null) {
				if (!phaseJustSet) {
					form1.alpha = ((float)(form1.n - 1)) * (float)numPhase.Value / 2.0f;
				} else {
					phaseJustSet = false;
				}
			}
		}

		private void numPhase_ValueChanged(object sender, System.EventArgs e) {
			numPhaseCahnged();
		}


		private void PositionForm_Deactivate(object sender, System.EventArgs e) {
			form1.n = (int)unmNSlits.Value;
			form1.alpha = ((float)(form1.n - 1)) * (float)numPhase.Value / 2.0f;
		}

		private void numPhase_Leave(object sender, System.EventArgs e) {
			numPhaseCahnged();
		}
	
	}
}
